﻿// movement
var WIGGLEMAXFRAME:Number = 1;
var FOOD_WAIT:Number = 5;

var foodCycle:Number = 0;

moveAnt = function(myAntAvatar:MovieClip, smartFox:SmartFoxClient){
	var left:Boolean = false;  //tells if you are going left
	var right:Boolean = false; //tells if you are going right
	var up:Boolean = false;    //tells if you are going up
	var down:Boolean = false;  //tells if you are going down

	// limits movement within map boundaries
//	if (_global.frameX >0 ) {_global.frameX = 0; }
//	if (_global.frameY >0 ) {_global.frameY = 0; }
//	if (_global.frameY < -(areaH-Stage.height)) {_global.frameY = -(areaH-Stage.height); }
//	if (_global.frameX < -(areaW-Stage.width)) {_global.frameX = -(areaW-Stage.width); }
		
	var px:Number = myAntAvatar._x+_global.frameX;
	var py:Number = myAntAvatar._y+_global.frameY;
		
	var downKey:Boolean = Key.isDown(Key.DOWN);
	var upKey:Boolean = Key.isDown(Key.UP);
	var leftKey:Boolean = Key.isDown(Key.LEFT);
	var rightKey:Boolean = Key.isDown(Key.RIGHT);
	
	var avatarFrame:Number = myAntAvatar._currentframe;
	
	if(avatarFrame == 2){
		curFrame %= WIGGLEMAXFRAME;
		curFrame++;
//		trace(curFrame);
	}
	
	if(foodCycle != FOOD_WAIT){
		foodCycle++;
//		trace(foodCycle);
	}
		
	if (downKey || upKey || leftKey || rightKey){
			//tells you what direction you are going in.
		if (downKey) {
			//trace("down is pressed");
			down = true;
		}
		if (upKey) {
			if(!down){
				up = true;
			} else {
				down = false;
			}
		}
	
		if (leftKey) {
			left = true;
		}
		if (rightKey) {
			if(!left){
				right = true;
			} else {
				left = false;
			}
		}
	
		if (curFrame == 1) {
			//you know you can only be going either up or down and either left or right
			if (down) {  //know that you're really going down.
				if(left){
					rotate = 225;
					moveGround(-speed, 0);
				} else if (right){
					rotate = 135;
					moveGround(speed, 0);
				} else {
					rotate = 180;
				}
				moveGround(0, speed);
				myAntAvatar.gotoAndStop(2);
			} else if (up){  //if you're going upwards
				if(left){  //going up-left
					rotate = 315;
					moveGround(-speed, 0);
				} else if (right){
					rotate = 45;
					moveGround(speed, 0);
				} else {
					rotate = 0;
				}
				moveGround(0, -speed);
				myAntAvatar.gotoAndStop(2);
			} else if(right){  //if you're going only right
				rotate = 90;
				moveGround(speed, 0);
				myAntAvatar.gotoAndStop(2);
			} else if(left){  //if you're only going left
				rotate = 270;
				moveGround(-speed, 0);
				myAntAvatar.gotoAndStop(2);
			} 
		
			myAntAvatar._rotation = rotate;
			px=_global.frameX+locX;
			py=_global.frameY+locY;

			myAntAvatar._rotation = rotate;
			smartfox.setUserVariables({px:px, py:py, rotateAngle:rotate, init:false});

			myAntAvatar._x = locX;
			myAntAvatar._y = locY;

			info.text = px+","+py;
		}
	} else{
		myAntAvatar.gotoAndStop(1);
		curFrame = 1;
	}
	
	if (Key.isDown(Key.SPACE) && (foodCycle == FOOD_WAIT)) {
		var nearest_food_index = nearestFoodIndex(px, py);
		if ((foodIndexArray.length >= maxCarriedFood) || (foodIndexArray.length>0 && nearest_food_index<=0)) {//ant is carrying maximum number of food
			var foodNum:Number = foodIndex;
			releaseFood();
			checkFoodInColony(foodNum);
			foodCycle = 0;			
			
		}else {
			pickUpFood(px, py, nearest_food_index);
			foodCycle = 0;
		}
		
/*		if(foodIndex==-1){//No food
			pickUpFood(px,py);
			foodCycle = 0;
		}else {
			var foodNum:Number = foodIndex;
			releaseFood();
			checkFoodInColony(foodNum);
			foodCycle = 0;
		}*/
	}
	speed = maximumSpeed - (foodIndexArray.length * decreasedSpeedForFood);
	updateRoom();
}

function moveGround(xDistance:Number, yDistance:Number){
	_global.frameX += xDistance;
	_global.frameY += yDistance;	
	
	if((ground_mc._x <= (-800 + speed)) || (ground_mc._x >= (0 - speed))){
		ground_mc._x = -400;
	}
	if((ground_mc._y <= (-600 + speed)) || (ground_mc._y >= (0 - speed))){
		ground_mc._y = -300;
	} else {
		ground_mc._x -= xDistance;
		ground_mc._y -= yDistance;
	}
}